package ControlLayer;
import java.util.ArrayList;
import ModelLayer.*; //import all Class' in ModelLayer

/* Controller class for the DVD collection
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * @revised version English (Ab)
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class LoanCtr
{
    //declare instance variables to reference containers
    private LoanContainer loanCon;
    private static LoanCtr instance;
    private AddressBookCtr abCtr;
    private DVDCtr dvdCtr;
    private int id;

    /**
     * Constructor for objects of class LoanCtr
     */
    public LoanCtr()
    {
        loanCon = LoanContainer.getInstance();
        abCtr = AddressBookCtr.getInstance();
        dvdCtr = DVDCtr.getInstance();
        id = 0;
    }
    
    public static LoanCtr getInstance()
    {
        if(instance == null)
        {
            instance = new LoanCtr();
        }
        return instance;
    }

    //Dummy implementation: after implementing the model layer 
    //objects of class DVD are to be returned, not String
    public Loan findLoan(int no)
    {
        return loanCon.findLoan(no); 
    }

    //Dummy implementation: more parameters are probaly to added
    public void createLoan(int no1, int no2, long no3, String loandate)
    {       
        id++;  
        DVD dvd = dvdCtr.findDVD(no1);
        Copy copy = dvdCtr.findCopy(no1, no2);
        Person person = abCtr.getPerson(no3);
        Loan loan = new Loan(id, dvd, copy, person, loandate);
        loanCon.addLoan(loan);
    }
    
    public void updateLoan(int no1, int no2, int no3, long no4, String loandate, String returndate, boolean active)
    {
        DVD dvd = dvdCtr.findDVD(no2);
        Copy copy =  dvdCtr.findCopy(no2, no3);
        Person person = abCtr.getPerson(no4);
        loanCon.updateLoan(no1, dvd, copy, person, loandate, returndate, active);
    }
    
    public int getID()
    {
        return id;
    }
    
    public void setID(int id)
    {
        this.id = id;
    }

    public void deleteLoan(int id)
    {
        loanCon.deleteLoan(id);
        //search for the loan and delete the object from the container
    }

    public void listAllLoans()
    {
        loanCon.printDetailAll();
    }
    
    public void returnLoan(int no, String returndate)
    {
        Loan loan = loanCon.findLoan(no);
        loan.setActive(false);
        loan.setReturndate(returndate);
    }
    
    public int getSizeLoan()
    {
        return loanCon.getSizeOfLoan();
    }
    
    public int getSizeOfDVD()
    {
        return dvdCtr.getSize();
    }
    
    public int getSizeOfPers()
    {
        return abCtr.getSize();
    }
    
    public DVD findDVD(int no)
    {
        return dvdCtr.findDVD(no);
    }
    
    public Copy findCopy(int no1, int no2)
    {         
        return dvdCtr.findDVD(no1).findCopy(no2);
    }
    
    public Person findPerson(long id)
    {        
        return abCtr.getPerson(id);
    }
}
